package com.java110.mall.goods.api;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.Environment;
import com.java110.dto.housekeeping.HousekeepingResourceDto;
import com.java110.po.housekeeping.HousekeepingFeeConfigPo;
import com.java110.po.housekeeping.HousekeepingResourcePo;
import com.java110.mall.goods.bmo.housekeepingResource.IDeleteHousekeepingResourceBMO;
import com.java110.mall.goods.bmo.housekeepingResource.IGetHousekeepingResourceBMO;
import com.java110.mall.goods.bmo.housekeepingResource.ISaveHousekeepingResourceBMO;
import com.java110.mall.goods.bmo.housekeepingResource.IUpdateHousekeepingResourceBMO;
import com.java110.po.housekeeping.HousekeepingResourceFeePo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/housekeepingResource")
public class HousekeepingResourceApi {

    @Autowired
    private ISaveHousekeepingResourceBMO saveHousekeepingResourceBMOImpl;
    @Autowired
    private IUpdateHousekeepingResourceBMO updateHousekeepingResourceBMOImpl;
    @Autowired
    private IDeleteHousekeepingResourceBMO deleteHousekeepingResourceBMOImpl;

    @Autowired
    private IGetHousekeepingResourceBMO getHousekeepingResourceBMOImpl;

    /**
     * 微信保存消息模板
     * @serviceCode /housekeepingResource/saveHousekeepingResource
     * @path /app/housekeepingResource/saveHousekeepingResource
     * @param reqJson
     * @return
     */
    @RequestMapping(value = "/saveHousekeepingResource", method = RequestMethod.POST)
    public ResponseEntity<String> saveHousekeepingResource(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");
        Assert.hasKeyAndValue(reqJson, "resTypeId", "请求报文中未包含resTypeId");
        Assert.hasKeyAndValue(reqJson, "resTypeName", "请求报文中未包含resTypeName");
        Assert.hasKeyAndValue(reqJson, "resName", "请求报文中未包含resName");
        Assert.hasKeyAndValue(reqJson, "resCode", "请求报文中未包含resCode");
        Assert.hasKeyAndValue(reqJson, "costPrice", "请求报文中未包含costPrice");
        Assert.hasKeyAndValue(reqJson, "price", "请求报文中未包含price");
        Assert.hasKeyAndValue(reqJson, "stock", "请求报文中未包含stock");
        Assert.hasKeyAndValue(reqJson, "supplierId", "请求报文中未包含supplierId");
        Assert.hasKeyAndValue(reqJson, "supplierName", "请求报文中未包含supplierName");
        Assert.hasKeyAndValue(reqJson, "feeTypeCd", "请求报文中未包含feeTypeCd");

        HousekeepingResourcePo housekeepingResourcePo = BeanConvertUtil.covertBean(reqJson, HousekeepingResourcePo.class);
        //物品默认费用明细信息
        HousekeepingFeeConfigPo housekeepingFeeConfigPo = new HousekeepingFeeConfigPo();
        housekeepingFeeConfigPo.setShopId(reqJson.getString("shopId"));
        housekeepingFeeConfigPo.setComputingFormula("2002");
        housekeepingFeeConfigPo.setFeeName(reqJson.getString("resName"));
        housekeepingFeeConfigPo.setFeePrice(reqJson.getString("price"));
        housekeepingFeeConfigPo.setFeeTypeCd(reqJson.getString("feeTypeCd"));
        housekeepingFeeConfigPo.setStartTime(DateUtil.getNow("yyyy-MM-dd HH:mm:ss"));
        housekeepingFeeConfigPo.setEndTime("2050-01-01");
        housekeepingFeeConfigPo.setIsDefault("F");
        //物品与费用明细关系表信息
        HousekeepingResourceFeePo housekeepingResourceFeePo = new HousekeepingResourceFeePo();

        return saveHousekeepingResourceBMOImpl.save(housekeepingResourcePo,housekeepingFeeConfigPo,housekeepingResourceFeePo);
    }

    /**
     * 微信修改消息模板
     * @serviceCode /housekeepingResource/updateHousekeepingResource
     * @path /app/housekeepingResource/updateHousekeepingResource
     * @param reqJson
     * @return
     */
    @RequestMapping(value = "/updateHousekeepingResource", method = RequestMethod.POST)
    public ResponseEntity<String> updateHousekeepingResource(@RequestBody JSONObject reqJson) {
        Environment.isDevEnv();
        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");
        Assert.hasKeyAndValue(reqJson, "resTypeId", "请求报文中未包含resTypeId");
        Assert.hasKeyAndValue(reqJson, "resTypeName", "请求报文中未包含resTypeName");
        Assert.hasKeyAndValue(reqJson, "resName", "请求报文中未包含resName");
        Assert.hasKeyAndValue(reqJson, "resCode", "请求报文中未包含resCode");
        Assert.hasKeyAndValue(reqJson, "costPrice", "请求报文中未包含costPrice");
        Assert.hasKeyAndValue(reqJson, "price", "请求报文中未包含price");
        Assert.hasKeyAndValue(reqJson, "stock", "请求报文中未包含stock");
        Assert.hasKeyAndValue(reqJson, "supplierId", "请求报文中未包含supplierId");
        Assert.hasKeyAndValue(reqJson, "supplierName", "请求报文中未包含supplierName");
        Assert.hasKeyAndValue(reqJson, "resId", "resId不能为空");
        Assert.hasKeyAndValue(reqJson, "configId", "configId不能为空");

        //物品默认费用明细信息
        HousekeepingFeeConfigPo housekeepingFeeConfigPo = new HousekeepingFeeConfigPo();
        housekeepingFeeConfigPo.setConfigId(reqJson.getString("configId"));
        housekeepingFeeConfigPo.setShopId(reqJson.getString("shopId"));
        housekeepingFeeConfigPo.setFeeName(reqJson.getString("resName"));
        housekeepingFeeConfigPo.setFeePrice(reqJson.getString("price"));

        HousekeepingResourcePo housekeepingResourcePo = BeanConvertUtil.covertBean(reqJson, HousekeepingResourcePo.class);


        return updateHousekeepingResourceBMOImpl.update(housekeepingResourcePo,housekeepingFeeConfigPo);
    }

    /**
     * 微信删除消息模板
     * @serviceCode /housekeepingResource/deleteHousekeepingResource
     * @path /app/housekeepingResource/deleteHousekeepingResource
     * @param reqJson
     * @return
     */
    @RequestMapping(value = "/deleteHousekeepingResource", method = RequestMethod.POST)
    public ResponseEntity<String> deleteHousekeepingResourllce(@RequestBody JSONObject reqJson) {
        Environment.isDevEnv();
        Assert.hasKeyAndValue(reqJson, "shopId", "商铺ID不能为空");
        Assert.hasKeyAndValue(reqJson, "resId", "resId不能为空");
        Assert.hasKeyAndValue(reqJson, "configId", "configId不能为空");
        Assert.hasKeyAndValue(reqJson, "resFeeId", "resFeeId不能为空");

        //物品默认费用明细信息
        HousekeepingFeeConfigPo housekeepingFeeConfigPo = new HousekeepingFeeConfigPo();
        housekeepingFeeConfigPo.setShopId(reqJson.getString("shopId"));
        housekeepingFeeConfigPo.setConfigId(reqJson.getString("configId"));
        //物品与费用明细关系表信息
        HousekeepingResourceFeePo housekeepingResourceFeePo = new HousekeepingResourceFeePo();
        housekeepingResourceFeePo.setResFeeId(reqJson.getString("resFeeId"));
        housekeepingResourceFeePo.setShopId(reqJson.getString("shopId"));

        HousekeepingResourcePo housekeepingResourcePo = BeanConvertUtil.covertBean(reqJson, HousekeepingResourcePo.class);
        return deleteHousekeepingResourceBMOImpl.delete(housekeepingResourcePo,housekeepingFeeConfigPo,housekeepingResourceFeePo);
    }

    /**
     * 微信删除消息模板
     * @serviceCode /housekeepingResource/queryHousekeepingResource
     * @path /app/housekeepingResource/queryHousekeepingResource
     * @param shopId 商铺ID
     * @return
     */
    @RequestMapping(value = "/queryHousekeepingResource", method = RequestMethod.GET)
    public ResponseEntity<String> queryHousekeepingResource(@RequestParam(value = "shopId") String shopId,
                                                            @RequestParam(value = "cartId", required = false) String cartId,
                                                            @RequestParam(value = "resTypeId", required = false) String resTypeId,
                                                            @RequestParam(value = "resTypeName", required = false) String resTypeName,
                                                            @RequestParam(value = "resName", required = false) String resName,
                                                            @RequestParam(value = "supplierName", required = false) String supplierName,
                                                      @RequestParam(value = "page") int page,
                                                      @RequestParam(value = "row") int row) {
        HousekeepingResourceDto housekeepingResourceDto = new HousekeepingResourceDto();
        housekeepingResourceDto.setPage(page);
        housekeepingResourceDto.setRow(row);
        housekeepingResourceDto.setShopId(shopId);
        housekeepingResourceDto.setResTypeId(resTypeId);
        housekeepingResourceDto.setResTypeName(resTypeName);
        housekeepingResourceDto.setResName(resName);
        housekeepingResourceDto.setSupplierName(supplierName);

        return getHousekeepingResourceBMOImpl.get(housekeepingResourceDto);
    }
}
